//index.js
import Notify from '../../dist/notify/notify';

Page({
  data: {
    //用户信息
    userInfo:{},
    //是否显示日期pick控件
    datePickShow:false,
    //当前日期 -> 用于 日期控件
    currentDate: new Date().getTime(),
    //最小日期 -> 用于 日期控件
    minDate: new Date().getTime(),

    //姓名
    name:'',
    //手机号
    phone:'',
    //日期格式化
    dateValue:'',
  },

  onLoad: function() {
    
  },

  //打开日期选择控件
  showDatePick(){
    this.setData({
      datePickShow:true
    })
  },
  
  //关闭日期选择控件
  closeDatePick(){
    this.setData({
      datePickShow:false
    })
  },

  //选择日期后
  pickDate(event){
    var dateString = this.formatDateStr(new Date(event.detail));
    this.setData({
      dateValue:dateString,
      datePickShow:false
    })
  },

  // 选项格式化函数
  formatDateStr:function(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    return year + "年" + month + "月" + day + "日"
  },


  //点击 -> 立即报名
  toSignUp(e){
    var that = this;
    //先做输入校验 -> 校验 姓名 + 手机号 + 预约日期
    var name = that.data.name;
    var phone = that.data.phone;
    var date = that.data.dateValue;
    if(name.length == 0){
      Notify({ 
        type: 'warning',
        message: '请输入姓名' ,
        duration:1000
      });
      return;
    }
    if(phone.length != 11){
      Notify({ 
        type: 'warning',
        message: '请输入正确的手机号' ,
        duration:1000
      });
      return;
    }
    if(date.length == 0){
      Notify({ 
        type: 'warning',
        message: '请选择日期' ,
        duration:1000
      });
      return;
    }
    var openid = wx.getStorageSync('openid');
    if(openid.length == 0){
      //用户未登录 -> 执行登录操作(获取用户信息)
      wx.getUserProfile({
        desc: '授权登录',
        success(res){
          that.setData({
            userInfo: res.userInfo
          })
          //将用户信息存入缓存中
          wx.setStorageSync('userInfo', res.userInfo);
          // 调用云函数 -> 获取用户openid
          wx.cloud.callFunction({
            name: 'login',
            success: res => {
              wx.setStorageSync('openid', res.result.openid)
            }
          });

          //登录完成 -> 将数据存入云数据库 -> 提示预约成功
          wx.cloud.callFunction({
            name: 'insertData',
            data:{
              nickName: that.data.userInfo.nickName,
              name: that.data.name,
              phone: that.data.phone,
              date: that.data.dateValue
            }
          });
          Notify({ 
            type: 'success',
            message: '预约成功' ,
            duration:1000,
            onClose(){
              that.setData({
                name:'',
                phone:'',
                dateValue:''
              })
            }
          });
        }
      })
    }else{
      //用户已登录 -> 校验信息并存入云数据库中 -> 提示预约成功
      wx.cloud.callFunction({
        name: 'insertData',
        data:{
          nickName: that.data.userInfo.nickName,
          name: that.data.name,
          phone: that.data.phone,
          date: that.data.dateValue
        }
      });
      Notify({ 
        type: 'success',
        message: '预约成功' ,
        duration:1000,
        onClose(){
          that.setData({
            name:'',
            phone:'',
            dateValue:''
          })
        }
      });
    }
  },



  //我是管理员
  toManagerPage(){
    wx.reLaunch({
      url: '/pages/manager/manager',
    })
  }
})
